const express = require('express');
const router = express.Router();
const db = require('../db');

// 获取所有排班类型
router.get('/', (req, res) => {
  db.all('SELECT * FROM shift_types ORDER BY id DESC', (err, rows) => {
    if (err) {
      res.status(500).json({ error: err.message });
      return;
    }
    res.json(rows);
  });
});

// 获取单个排班类型
router.get('/:id', (req, res) => {
  const id = req.params.id;
  db.get('SELECT * FROM shift_types WHERE id = ?', [id], (err, row) => {
    if (err) {
      res.status(500).json({ error: err.message });
      return;
    }
    if (!row) {
      res.status(404).json({ message: '排班类型不存在' });
      return;
    }
    res.json(row);
  });
});

// 创建排班类型
router.post('/', (req, res) => {
  const { name, cycle, holiday_work_schedule, adjust_work_day_schedule, applicable_type, work_time } = req.body;
  const sql = `INSERT INTO shift_types (name, cycle, holiday_work_schedule, adjust_work_day_schedule, applicable_type, work_time)
               VALUES (?, ?, ?, ?, ?, ?)`;
  db.run(sql, [name, cycle, holiday_work_schedule, adjust_work_day_schedule, applicable_type, work_time || ''], function(err) {
    if (err) {
      res.status(500).json({ error: err.message });
      return;
    }
    res.status(201).json({
      id: this.lastID,
      message: '排班类型创建成功',
      data: { id: this.lastID, name, cycle, holiday_work_schedule, adjust_work_day_schedule, applicable_type }
    });
  });
});

// 更新排班类型
router.put('/:id', (req, res) => {
  const id = req.params.id;
  const body = req.body;
  
  // 构建更新字段和参数
  const fields = [];
  const params = [];
  
  if (body.name !== undefined) {
    fields.push('name = ?');
    params.push(body.name);
  }
  if (body.cycle !== undefined) {
    fields.push('cycle = ?');
    params.push(body.cycle);
  }
  if (body.holiday_work_schedule !== undefined) {
    fields.push('holiday_work_schedule = ?');
    params.push(body.holiday_work_schedule);
  }
  if (body.adjust_work_day_schedule !== undefined) {
    fields.push('adjust_work_day_schedule = ?');
    params.push(body.adjust_work_day_schedule);
  }
  if (body.applicable_type !== undefined) {
    fields.push('applicable_type = ?');
    params.push(body.applicable_type);
  }
  if (body.work_time !== undefined) {
    fields.push('work_time = ?');
    params.push(body.work_time);
  }
  if (body.is_enabled !== undefined) {
    fields.push('is_enabled = ?');
    params.push(body.is_enabled);
  }
  
  // 确保至少有一个字段被更新
  if (fields.length === 0) {
    res.status(400).json({ error: '至少需要更新一个字段' });
    return;
  }
  
  // 添加更新时间
  fields.push('updated_at = CURRENT_TIMESTAMP');
  
  // 构建SQL语句
  const sql = `UPDATE shift_types SET ${fields.join(', ')} WHERE id = ?`;
  params.push(id);
  
  db.run(sql, params, function(err) {
    if (err) {
      res.status(500).json({ error: err.message });
      return;
    }
    if (this.changes === 0) {
      res.status(404).json({ message: '排班类型不存在' });
      return;
    }
    res.json({ message: '排班类型更新成功' });
  });
});

// 删除排班类型
router.delete('/:id', (req, res) => {
  const id = req.params.id;
  db.run('DELETE FROM shift_types WHERE id = ?', [id], function(err) {
    if (err) {
      res.status(500).json({ error: err.message });
      return;
    }
    if (this.changes === 0) {
      res.status(404).json({ message: '排班类型不存在' });
      return;
    }
    res.json({ message: '排班类型删除成功' });
  });
});

module.exports = router;